﻿using Zeus.DAL.ADO;
using Zeus.Biz.MySql.Model;
using System.Collections.Generic;
using System.Data;
using MySql.Data.MySqlClient;
using Zeus.DAL.ADO.MySql;
using System.Configuration;
using Zeus.Biz.MySql;

namespace Zeus.Biz.MySql.Dao
{
    public class EmployeeDao : IDAO<Employee>
    {
        public bool Insert(Employee obj)
        {
            string sql = "INSERT INTO tbEmployee (name, age) VALUES(@name, @age)";

            MySqlDb db = new MySqlDb(ApplicationConfig.DbConnection);

            MySqlParameter[] paras = new MySqlParameter[] 
            { 
                new MySqlParameter("@name", obj.Name), 
                new MySqlParameter("@age", obj.Age) 
            };

            int result = db.ExecuteSqlParam(sql, paras);
            if (result == -1)
                return false;
            else
                return true;
        }

        public bool Update(Employee obj)
        {
            string sql = "UPDATE tbEmployee SET name=@name, age=@age WHERE id=@id";

            MySqlDb db = new MySqlDb(ApplicationConfig.DbConnection);

            MySqlParameter[] paras = new MySqlParameter[] 
            { 
                new MySqlParameter("@id", obj.Id), 
                new MySqlParameter("@name", obj.Name), 
                new MySqlParameter("@age", obj.Age) 
            };

            int result = db.ExecuteSqlParam(sql, paras);
            if (result == -1)
                return false;
            else
                return true;
        }

        public bool Delete(int id)
        {
            string sql = "DELETE FROM tbEmployee WHERE id=@id";

            MySqlDb db = new MySqlDb(ApplicationConfig.DbConnection);

            MySqlParameter[] paras = new MySqlParameter[] 
            { 
                new MySqlParameter("@id", id)
            };

            int result = db.ExecuteSqlParam(sql, paras);
            if (result == -1)
                return false;
            else
                return true;
        }

        public Employee GetSingle(int id)
        {
            string sql = "SELECT * FROM tbEmployee where id=@id";
            Employee employee = null;

            MySqlDb db = new MySqlDb(ApplicationConfig.DbConnection);

            MySqlParameter[] paras = new MySqlParameter[] 
            { 
                new MySqlParameter("@id", id)
            };

            MySqlDataReader dr = db.GetDataReaderParam(sql, paras);

            if (dr.HasRows)
            {
                dr.Read();
                employee = new Employee
                {
                    Id = int.Parse(dr["id"].ToString()),
                    Name = dr["Name"].ToString(),
                    Age = int.Parse(dr["Age"].ToString())
                };
            }

            return employee;
        }

        public IList<Employee> GetAll()
        {
            string sql = "SELECT * FROM tbEmployee";
            IList<Employee> employeeList = null;
            Employee employee = null;

            MySqlDb db = new MySqlDb(ApplicationConfig.DbConnection);
            MySqlDataReader dr = db.GetDataReader(sql);

            if (dr.HasRows)
            {
                employeeList = new List<Employee>();
                while (dr.Read())
                {
                    employee = new Employee
                    {
                        Id = int.Parse(dr["id"].ToString()),
                        Name = dr["Name"].ToString(),
                        Age = int.Parse(dr["Age"].ToString())
                    };

                    employeeList.Add(employee);
                }
            }

            return employeeList;
        }

        public IList<Employee> GetByCond(string cond)
        {
            return null;
        }

        public DataSet GetDsAll()
        {
            return null;
        }

        public DataSet GetDsByCond(string cond)
        {
            return null;
        }
    }
}
